package cn.demo.Puzzle;

import java.awt.Panel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.*;

/**
 * ????????????????
 *
 * @author HP
 */

public class GamePanel extends Panel implements MouseListener {

    private static final long serialVersionUID = 6334922490608984691L;
    private Cell[] cells = new Cell[9];
    private Cell cellBlank = null;
    private final Cell cellsuccess = new Cell(new ImageIcon(GamePanel.class.getResource("1010.jpg")), 4);// ????????
    private static JButton Start = new JButton();
    private static JButton see = new JButton("????????");

    public GamePanel() {
        // TODO Auto-generated constructor stub
        super();
        setLayout(null);
        init();
        setMouserListener();
        initStartButton();
        initSeeButton();
    }

    /*
     * ??????????(non-Javadoc)
     * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
     */
    public void init() {
        int num = 0, c = 0;
        ImageIcon icon = null;
        Cell cell = null;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                num = i * 3 + j;
                icon = new ImageIcon(GamePanel.class.getResource((num) + ".jpg"));
                cell = new Cell(icon, num);// ??????????
                cell.setLocation(j * Cell.IMAGEWIDTH, i * Cell.IMAGEWIDTH);// ??????????????????
                cells[c++] = cell;// ??????????????????????????????
            }
        }
        for (int i = 0; i < cells.length; i++) {
            this.add(cells[i]);
        }
        this.add(Start);
    }

    /*
     *????????
     */
    public void initSeeButton() {
        see.setBounds(650, 300, 100, 25);
        see.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                YuanImage.initYuanImage();
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        this.add(see);
    }

    /*
     *????????????????
     */
    public void initStartButton() {
        Start.setText("????????");
        Start.setBounds(650, 250, 100, 25);
        Start.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                random();
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

    /*
     *??????????
     */
    public void random() {
        Random random = new Random();
        int m, count = 0;
        while (count < 500) {
            m = random.nextInt(9);
            swap(cells[m]);
            count++;
        }
        Start.setSelected(false);
    }

    /*
     *????
     */
    public void setMouserListener() {
        if (cellBlank == null) {
            cellBlank = cells[cells.length - 1];// ????????????
            for (int i = 0; i < cells.length; i++) {
                if (i != cells.length - 1) {
                    cells[i].addMouseListener(this);// ????????????????????????
                }
            }
        }
    }

    public void swap(Cell cell) {
        int x = cellBlank.getX();
        int y = cellBlank.getY();
        if ((x - cell.getX()) == Cell.IMAGEWIDTH && cell.getY() == y) {
            cell.move(Direction.RIGHT);
            cellBlank.move(Direction.LEFT);
        } else if ((x - cell.getX()) == -Cell.IMAGEWIDTH && cell.getY() == y) {
            cell.move(Direction.LEFT);
            cellBlank.move(Direction.RIGHT);
        } else if (cell.getX() == x && (cell.getY() - y) == Cell.IMAGEWIDTH) {
            cell.move(Direction.UP);
            cellBlank.move(Direction.DOWN);
        } else if (cell.getX() == x && (cell.getY() - y) == -Cell.IMAGEWIDTH) {
            cell.move(Direction.DOWN);
            cellBlank.move(Direction.UP);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        Cell cell = (Cell) e.getSource();// ??????????????????
        int x = cellBlank.getX();
        int y = cellBlank.getY();
        if ((x - cell.getX()) == Cell.IMAGEWIDTH && cell.getY() == y) {
            cell.move(Direction.RIGHT);
            cellBlank.move(Direction.LEFT);
        } else if ((x - cell.getX()) == -Cell.IMAGEWIDTH && cell.getY() == y) {
            cell.move(Direction.LEFT);
            cellBlank.move(Direction.RIGHT);
        } else if (cell.getX() == x && (cell.getY() - y) == Cell.IMAGEWIDTH) {
            cell.move(Direction.UP);
            cellBlank.move(Direction.DOWN);
        } else if (cell.getX() == x && (cell.getY() - y) == -Cell.IMAGEWIDTH) {
            cell.move(Direction.DOWN);
            cellBlank.move(Direction.UP);
        }
        if (isSuccess()) {
            cellBlank.setStateIcon(new ImageIcon(GamePanel.class.getResource("1010.jpg")));
            this.repaint();
            int i = JOptionPane.showConfirmDialog(this, "??????????????????", "??????????", JOptionPane.YES_NO_OPTION);
            if (i == JOptionPane.YES_OPTION) {
                cellBlank.setStateIcon(new ImageIcon(GamePanel.class.getResource("8.jpg")));
                Start.setSelected(true);
                random();
            } else if (i == JOptionPane.NO_OPTION) {
                System.exit(0);
            }
        }
    }

    /*
     * ????????????????(non-Javadoc)
     * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
     */
    public boolean isSuccess() {
        for (int i = 0; i < cells.length; i++) {
            int x = cells[i].getX();
            int y = cells[i].getY();
            if (i != 0) {
                if ((y / Cell.IMAGEWIDTH * 3 + x / Cell.IMAGEWIDTH) != cells[i].getPlace()) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}
